package Service;

import Entity.User;
import GUI.showMessageFrame;
import Monitor.ClientThread;
import Utils.PacketUtils;
import Utils.ThreadManage;
import org.json.JSONObject;

import java.io.IOException;


/**
 * The type Add friend.
 *
 * @author dzr
 * @version 1.0
 */
public class AddFriend {
    /**
     * The constant ADD_FRI.
     */
    public static final int ADD_FRI = 2;
    /**
     * The constant FRI_ALREADY.
     */
    public static final int FRI_ALREADY = 123;
    /**
     * The constant IS_YOURSELF.
     */
    public static final int IS_YOURSELF = 222;
    /**
     * The constant USER_NULL.
     */
    public static final int USER_NULL = 404;
    /**
     * The constant ADD_SUC.
     */
    public static final int ADD_SUC = 696;

    /**
     * Add friend.
     *
     * @param s_uid the s uid
     * @param f_uid the f uid
     * @throws IOException the io exception
     */
    public static void addFri(int s_uid, int f_uid) throws IOException {
        JSONObject ret2Server = new JSONObject();
        ClientThread thread = ThreadManage.getInstance().getClientThreads().get(s_uid);

        ret2Server.put("cmd", ADD_FRI);
        ret2Server.put("s_uid", s_uid);
        ret2Server.put("r_uid", f_uid);
        PacketUtils.sendPacket(ret2Server);
    }

    /**
     * To parse the result.
     *
     * @param jsonObject the json object of result
     */
    public static void result(JSONObject jsonObject){
        int ret_code = jsonObject.getInt("ret_code");
        switch (ret_code){
            case IS_YOURSELF:
                new showMessageFrame("不能添加自己为好友哦！");
                break;
            case FRI_ALREADY:
                new showMessageFrame("你们已经是好友啦！");
                break;
            case USER_NULL:
                new showMessageFrame("你要添加的用户不存在哦！");
                break;
            case ADD_SUC:
                if(jsonObject.getInt("type") == 1){
                    if (!jsonObject.getBoolean("r_status")){
                        new showMessageFrame("好友添加成功，可惜好友当前不在线！");
                    }else{
                        ClientThread clientThread = ThreadManage.getInstance().getClientThreads().get(jsonObject.getInt("s_uid"));
                        User newFriend = new User(jsonObject.getInt("r_uid"),
                                jsonObject.getInt("r_profileID"),
                                jsonObject.getString("r_name"),
                                jsonObject.getString("r_sex"),
                                jsonObject.getString("r_birthday"));
                        clientThread.getFriends_online().put(newFriend.getUid(), newFriend);
                        new showMessageFrame("好友添加成功，刚好好友当前在线！");
                        clientThread.getMainFrame().updateFriendStatus(newFriend, true, false);
                    }
                }else{
                    ClientThread clientThread = ThreadManage.getInstance().getClientThreads().get(jsonObject.getInt("r_uid"));
                    User newFriend = new User(jsonObject.getInt("s_uid"),
                            jsonObject.getInt("s_profileID"),
                            jsonObject.getString("s_name"),
                            jsonObject.getString("s_sex"),
                            jsonObject.getString("s_birthday"));
                    clientThread.getFriends_online().put(newFriend.getUid(), newFriend);
                    new showMessageFrame("您有新的好友啦！" + newFriend.getUsername() + "(" + newFriend.getUid() + ")");
                    clientThread.getMainFrame().updateFriendStatus(newFriend, true, false);
                }
                break;
            default:
                break;
        }
    }
}
